Create a Calculated Attribute

A Calculated Attribute is a special type of Member Attribute whose value is dynamically derived using an expression, instead of being directly specified. The Calculated Attribute holds the result of the expression as it was last computed. Use Calculated Attributes to automatically calculate values, such as member lifetime spend, average order value, or reward eligibility, without manual updates.

At a minimum, a Calculated Attributed is defined by the following:

  • Name: The display name for the Attribute.

  • Data Type: Defines the type of data stored in the Attribute (see Glossary: Data Types for more information).

  • Expression: The logic that defines how this value is derived. You can provide this expression by either: directly entering the expression code, or configuring it through the platform's user interface.

  • Time-to-Live (TTL): For API requests, a duration that indicates how long the calculated value is considered valid before it needs to be re-calculated.

  • Computation Mode: Method that determines when the Attribute value is recalculated. The available computation modes are:

    • On-demandOn-demand computation happens when some event or caller needs to display the value in the Calculated Attribute for a Member. This caller could be, for example, a mobile app, a website, an API request, or a user within the platform's interface. For API requests, the platform checks the Time-to-Live value for the Calculated Attribute for the referenced Member. If the current time is beyond the TTL range, the platform will recalculate the value in the Calculated Attribute. Conversely, if the current time is still within the TTL range, the platform will simply show the existing cached value.

    • ScheduledScheduled computation occurs based on a Scheduled Job. When the job executes, it recalculates the values in the Calculated Attribute for all Members in your database.

The Loyalty platform supports the use of Groovy for writing Calculated Attribute expressions. Groovy is a scripting language compatible with Java. Groovy lets you reference Member Attributes, Activity Attributes, and Member Functions.

Note: You must have a solid understanding of the Groovy language in order to write Calculated Attribute expressions. For more information on Groovy, see: https://groovy-lang.org/

To create a Calculated Attribute, you must first create a Member Attribute; see Create a Member Attribute for details on that process. When configuring the Member Attribute, be sure to select the following option:

  • Check Calculated to indicate that you are creating a Calculated Attribute.

After marking the Member Attribute as Calculated, complete its setup in the following tabs of the Member Attribute Details screen:

  • Settings: Define the Computation Mode, TTL, and other settings.

  • Variables: Optionally, define reusable variables for your expression.

  • Calculation Rules: Define the calculation logic.